﻿window.alert = function (name) {
    let iframe = document.createElement("IFRAME");
    iframe.style.display = "none";
    document.documentElement.appendChild(iframe);
    window.frames[0].window.alert(name);
    iframe.parentNode.removeChild(iframe);
};
window.confirm = function (message) {
    let iframe = document.createElement("IFRAME");
    iframe.style.display = "none";
    iframe.setAttribute("src", 'data:text/plain,');
    document.documentElement.appendChild(iframe);
    let alertFrame = window.frames[0];
    let result = alertFrame.window.confirm(message);
    iframe.parentNode.removeChild(iframe);
    return result;
};

let overallSituationData;
//查询医院
selectAjax("SelectByResponsibleHospital", "yiyuan");
//查询科室
selectAjax("SelectByResponsibleDepartment", "keshi")
selectAjax("SelectByResponsibleDepartment", "keshis");
//查询级别
selectAjax("SelectByLevel", "jibie");

function selectAjax(url, attribute) {
    $.ajax({
        url: "http://localhost:8989/hospital/FollowUpGroup/" + url,
        type: "post",
        dataType: "json",
        success: function (data) {
            for (let dataKey in data) {
                if (data[dataKey].responsibleHospital != undefined) {
                    let datas = data[dataKey].responsibleHospital;
                    $("#" + attribute).after('<option value=' + datas + '>' + datas + '</option>');
                }
                if (data[dataKey].responsibleDepartment != undefined) {
                    let datas = data[dataKey].responsibleDepartment;
                    $("#" + attribute).after("<option class='option' value=" + datas + ">" + datas + "</option>");
                }
                if (data[dataKey].level != undefined) {
                    let datas = data[dataKey].level;
                    $("#" + attribute).after("<option class='options' value=" + datas + ">" + datas + "</option>");
                }
            }
        }
    });
}

//页面加载完成加载
$(function () {
    //导航条引入
    $("#title").load("导航条.html");
});

//跳转建立分组页面
function NewFollowUp() {
    location.href = "建立分组.html";
}

//angular js
angular.module('fenZuGuanLi', []).controller('fenZuGuanLiView', function ($scope) {
    /**
     * checkbox全选/全不选
     */
    //给全选的复选框添加事件
    $(document).on("click", "#all", function () {
        // this 全选的复选框
        var userids = this.checked;
        //获取name=box的复选框 遍历输出复选框
        $("input[name=box]").each(function () {
            this.checked = userids;
        });
    });

    //给name=box的复选框绑定单击事件 在angular属性里jquery点击事件不触发 解决方法用on
    $(document).on("click", "input[name=box]", function () {
        //获取选中复选框长度
        var length = $("input[name=box]:checked").length;
        //未选中的长度
        var len = $("input[name=box]").length;
        if (length == len) {
            $("#all").get(0).checked = true;
        } else {
            $("#all").get(0).checked = false;
        }
    });

    //分页数据
    let data = {
        page: 1,
        pageNum: 5
    };

    // 上一页/下一页
    $scope.page = function (status) {
        //上一页
        if (status == "-1") {
            if (data.page > 1) {
                data.page = data.page - 1;
                //调用表格数据与分页数据函数
                $scope.TablesAndPagination();
            }
        }
        //下一页
        if (status == "+1") {
            if (data.page < $scope.pageData.remaining_pages) {
                data.page = data.page + 1;
                //调用表格数据与分页数据函数
                $scope.TablesAndPagination();
            }
        }
        //尾页
        if (status == "" || status == null) {
            data.page = $scope.pageData.remaining_pages;
            //调用表格数据与分页数据函数
            $scope.TablesAndPagination();
        }
        //跳转页
        if (status == "jump") {
            let val = $("#JumpPage").val();
            if (val == null || val == "") {
                alert("请输入跳转页");
                return;
            }
            if (val > $scope.pageData.remaining_pages) {
                alert("没有下一页了");
                return;
            }
            data.page = val;
            //调用表格数据与分页数据函数
            $scope.TablesAndPagination();
        }
    };
    //编辑方法
    $scope.selectByFollowUpGroupId = function () {
        let result = "";
        let count = 0;
        $(".check").each(function () {
            if ($(this).is(':checked')) {
                result += $(this).val();
                count++;
            }
        });
        if (count > 1 || count == 0) {
            alert("请选择一条数据进行编辑");
            return;
        } else {
            $("#myModal2").modal("show");
            $("#div").html("<span></span>");
            $("#buttons").html("<button onclick='update()' type='button' class='btn btn-info'>修改</button><button type='button' class='btn btn-default' data-dismiss='modal'>取消</button>");
        }
        $(".select").find("option").attr("selected", false);
        $(".select2").find("option").attr("selected", false);
        $.ajax({
            url: "http://localhost:8989/hospital/FollowUpGroup/SelectByFollowUpGroupId",
            type: "post",
            dataType: "json",
            data: {"id": result},
            success: function (data) {
                $scope.$apply(function () {
                    $scope.datas = data;
                    overallSituationData = $scope.datas;
                    //下拉菜单选中状态 频繁点击编辑先将下拉菜单设置未未选中状态 然后在设置选择状态避免设置不上选择值
                    $(".select").find("option[value='" + $scope.datas.responsibleDepartment + "']").prop("selected", true);
                    $(".select2").find("option[value='" + $scope.datas.level + "']").prop("selected", true);
                });
            }
        });
    };
    //删除方法
    $scope.remove = function (status) {
        let result = "";
        let count = 0;
        $(".check").each(function () {
            if ($(this).is(':checked')) {
                result += $(this).val() + ",";
                count++;
            }
        });
        if (status != null && count != 0) {
            if (!confirm("确定删除这" + count + "条随访组信息?")) {
                return;
            }
            bulkDelete();

            //批量删除方法
            function bulkDelete() {
                $.ajax({
                    url: "http://localhost:8989/hospital/FollowUpGroup/BulkDelete",
                    type: "post",
                    dataType: "json",
                    data: {"id": result},
                    success: function (status) {
                        if (status == true) {
                            //全选多选框设置未选中状态
                            $("#all").attr("checked", false);
                            //调用表格数据与分页数据函数
                            $scope.TablesAndPagination();
                        } else {
                            alert("删除失败,请重试");
                        }
                    }
                });
            }
        } else {
            alert("请至少选择一条信息进行删除")
        }
    };

    //表格数据与分页数据渲染
    $scope.TablesAndPagination = function () {
        $.ajax({
            url: "http://localhost:8989/hospital/FollowUpGroup/SelectAll",
            type: "post",
            dataType: "json",
            data: data,
            success: function (data) {
                for (let dataKey in data) {
                    let datas = data[dataKey];
                    $scope.$apply(function () {
                        $scope.pageData = datas;
                        if ($scope.pageData.count == 0) {
                            $("#column").html("<tr><td style=\"color: red\">没有数据</td></tr>");
                            $(".data").empty();
                        }
                    });
                    for (let i = 0; i <= datas.length - 1; i++) {
                        $scope.$apply(function () {
                            $scope.data = datas;
                        });
                    }
                }
                $(".spinner").css("display", "none");
                //更改获取的id值
                page("shangyes");
                page("xiayes");
                page("weiyes");
                page("tiaozhuanyes");

                page("shangye");
                page("xiaye");
                page("weiye");
                page("tiaozhuanye");

                //修改触发的点击事件
                function page(state) {
                    if (state == "shangyes") {
                        $("#" + state).css("display", "none");
                    } else if (state == "xiayes") {
                        $("#" + state).css("display", "none");
                    } else if (state == "weiyes") {
                        $("#" + state).css("display", "none");
                    } else if (state == "tiaozhuanyes") {
                        $("#" + state).css("display", "none");
                    } else if (state == "shangye") {
                        $("#" + state).css("display", "");
                    } else if (state == "xiaye") {
                        $("#" + state).css("display", "");
                    } else if (state == "weiye") {
                        $("#" + state).css("display", "");
                    } else if (state == "tiaozhuanye") {
                        $("#" + state).css("display", "");
                    }
                }
            }, error: function () {
                $("#column").html("<span style='color: #f8c815' class='glyphicon glyphicon-warning-sign'></span>&nbsp;<span style='color: red'>加载失败</span>");
                $(".spinner").css("display", "none");
            }
        })
    };
    //调用表格数据与分页数据函数
    $scope.TablesAndPagination();
    //分页模糊查询
    $scope.TablesAndPaginationLike = function (datas) {
        //添加页信息
        datas['page'] = data.page;
        datas['pageNum'] = data.pageNum;
        $.ajax({
            url: "http://localhost:8989/hospital/FollowUpGroup/SelectLikeCondition",
            type: "post",
            data: datas,
            dataType: "json",
            success: function (data) {
                for (let dataKey in data) {
                    let datas = data[dataKey];
                    $scope.$apply(function () {
                        $scope.pageData = datas;
                        if ($scope.pageData.count == 0) {
                            $("#column").html("<tr><td style=\"color: red\">没有数据</td></tr>");
                            $(".data").empty();
                        } else {
                            $("#column").empty();
                        }
                    });
                    for (let i = 0; i <= datas.length - 1; i++) {
                        $scope.$apply(function () {
                            $scope.data = datas;
                        });
                    }
                }
                //加载loading
                $(".spinner").css("display", "none");
            },
            error: function () {
                $("#column").html("<span style='color: #f8c815' class='glyphicon glyphicon-warning-sign'></span>&nbsp;<span style='color: red'>加载失败</span>");
                $(".spinner").css("display", "none");
            }
        });
        //更改获取的id值
        page("shangye");
        page("xiaye");
        page("weiye");
        page("tiaozhuanye");

        //更改获取的id值
        page("shangyes");
        page("xiayes");
        page("weiyes");
        page("tiaozhuanyes");
    };

    //修改触发的点击事件
    function page(state) {
        if (state == "shangye") {
            $("#" + state).css("display", "none");
        } else if (state == "xiaye") {
            $("#" + state).css("display", "none");
        } else if (state == "weiye") {
            $("#" + state).css("display", "none");
        } else if (state == "tiaozhuanye") {
            $("#" + state).css("display", "none");
        } else if (state == "shangyes") {
            $("#" + state).css("display", "");
        } else if (state == "xiayes") {
            $("#" + state).css("display", "");
        } else if (state == "weiyes") {
            $("#" + state).css("display", "");
        } else if (state == "tiaozhuanyes") {
            $("#" + state).css("display", "");
        }
    }

    // 上一页/下一页
    $scope.pages = function (status) {
        //上一页
        if (status == "-1") {
            if (data.page > 1) {
                data.page = data.page - 1;
                $scope.retrieval();
            }
        }
        //下一页
        if (status == "+1") {
            if (data.page < $scope.pageData.remaining_pages) {
                data.page = data.page + 1;
                $scope.retrieval();
            }
        }
        //尾页
        if (status == "" || status == null) {
            data.page = $scope.pageData.remaining_pages;
            $scope.retrieval();
        }
        //跳转页
        if (status == "jump") {
            let val = $("#JumpPage").val();
            if (val == null || val == "") {
                alert("请输入跳转页");
                return;
            }
            if (val > $scope.pageData.remaining_pages) {
                alert("没有下一页了");
                return;
            }
            data.page = val;
            $scope.retrieval();
        }
    };
    //检索方法
    $scope.retrieval = function () {
        //style样式
        let style = "style='color: #f8c815' class='glyphicon glyphicon-warning-sign'";
        let myStyle = "style='color: #2aabd2'";
        //负责人
        let fuzeren = $("#fuzeren").val();
        //科室
        let keshi = $(".option:selected").val();
        //状态
        let zhuangtai = $(".zhuangtai:selected").val();
        //开始日期
        let startDate = $("#startDate").val();
        //结束日期
        let endDate = $("#endDate").val();
        //日期比较
        let startDates = new Date(startDate);
        let endDates = new Date(endDate);
        //正则表达式验证
        if (!(/^[\u4e00-\u9fa5]{0,}$/.test(fuzeren))) {
            status = "请正确填写负责人";
            $("#state").html("<div " + style + "></div>&nbsp;" + "<span style='color: red'>" + status + "</span>");
            return;
        } else if ((startDate != "" && endDate == "") || (startDate == "" && endDate != "")) {
            status = "创建日期选择不完整";
            $("#state").html("<div " + style + "></div>&nbsp;" + "<span style='color: red'>" + status + "</span>");
            return;
        } else if (startDates > endDates) {
            status = "创建日期选择错误";
            $("#state").html("<div " + style + "></div>&nbsp;" + "<span style='color: #ff0000'>" + status + "</span>");
            return;
        } else if ((fuzeren == null || fuzeren == "") && (keshi == "请选择" || keshi == undefined) && (zhuangtai == "请选择" || zhuangtai == undefined) && ((startDate == "" || startDate == null) && (endDate == null || endDate == ""))) {
            status = "请至少填写或选择一个条件";
            $("#state").html("<div " + style + "></div>&nbsp;" + "<span style='color: red'>" + status + "</span>");
            return;
        } else {
            status = "检索成功";
            $("#state").html("<div class='glyphicon glyphicon-warning-sign' style='color:#00cc03'></div>&nbsp;" + "<span " + myStyle + ">" + status + "</span>");
        }
        let data = {
            "personInCharge": fuzeren,
            "responsibleDepartment": keshi,
            "state": zhuangtai,
            "startDate": startDate,
            "endDate": endDate
        };
        //检索方法
        $scope.TablesAndPaginationLike(data);
    }
});

//修改方法
function update() {
    //style样式
    let style = "style='color: #f8c815' class='glyphicon glyphicon-warning-sign'";
    let styles = "style='color: red'";
    //错误提示信息
    let status;
    //下拉菜单选中值
    let val1 = $(".select option:selected").val();
    let val2 = $(".select2 option:selected").val();
    //全局变量
    overallSituationData.responsibleDepartment = val1;
    overallSituationData.level = val2;
    //日期比较
    let startDate = new Date(overallSituationData.startDate);
    let endDate = new Date(overallSituationData.endDate);
    //正则表达式判断输入信息
    if (overallSituationData.responsibleHospital == null || overallSituationData.responsibleHospital == "") {
        status = "请填写医院";
        $("#div").html("<div " + style + "></div>&nbsp;" + "<span " + styles + ">" + status + "</span>");
        return;
    } else if (!(/^[\u4e00-\u9fa5]{0,}$/.test(overallSituationData.responsibleHospital))) {
        status = "请正确填写医院";
        $("#div").html("<div " + style + "></div>&nbsp;" + "<span " + styles + ">" + status + "</span>");
        return;
    } else if (val1 == "请选择") {
        status = "请选择科室";
        $("#div").html("<div " + style + "></div>&nbsp;" + "<span " + styles + ">" + status + "</span>");
        return;
    } else if (overallSituationData.startDate == null || overallSituationData.startDate == "") {
        status = "请选择开始日期";
        $("#div").html("<div " + style + "></div>&nbsp;" + "<span " + styles + ">" + status + "</span>");
        return;
    } else if (overallSituationData.endDate == null || overallSituationData.endDate == "") {
        status = "请选择结束日期";
        $("#div").html("<div " + style + "></div>&nbsp;" + "<span " + styles + ">" + status + "</span>");
        return;
    } else if (startDate > endDate) {
        status = "开始日期不能大于结束日期";
        $("#div").html("<div " + style + "></div>&nbsp;" + "<span " + styles + ">" + status + "</span>");
        return;
    } else if (val2 == "请选择") {
        status = "请选择级别";
        $("#div").html("<div " + style + "></div>&nbsp;" + "<span " + styles + ">" + status + "</span>");
        return;
    } else if (overallSituationData.contactNumber == null || overallSituationData.contactNumber == "") {
        status = "请填写电话";
        $("#div").html("<div " + style + "></div>&nbsp;" + "<span " + styles + ">" + status + "</span>");
        return;
    } else if (!(/^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/.test(overallSituationData.contactNumber))) {
        status = "请正确填写电话";
        $("#div").html("<div " + style + "></div>&nbsp;" + "<span " + styles + ">" + status + "</span>");
        return;
    } else if (overallSituationData.personInCharge == null || overallSituationData.personInCharge == "") {
        status = "请填写负责人";
        $("#div").html("<div " + style + "></div>&nbsp;" + "<span " + styles + ">" + status + "</span>");
        return;
    } else if (!(/^[\u4e00-\u9fa5]{0,}$/.test(overallSituationData.personInCharge))) {
        status = "请正确填写负责人";
        $("#div").html("<div " + style + "></div>&nbsp;" + "<span " + styles + ">" + status + "</span>");
        return;
    } else if (overallSituationData.name == null || overallSituationData.name == "") {
        status = "请填写名称";
        $("#div").html("<div " + style + "></div>&nbsp;" + "<span " + styles + ">" + status + "</span>");
        return;
    } else if (!(/^[\u4E00-\u9FA5A-Za-z0-9]+$/.test(overallSituationData.name))) {
        status = "请正确填写名称";
        $("#div").html("<div " + style + "></div>&nbsp;" + "<span " + styles + ">" + status + "</span>");
        return;
    } else if (overallSituationData.numberOfPatients == null || overallSituationData.numberOfPatients == "") {
        status = "请填写患者数量";
        $("#div").html("<div " + style + "></div>&nbsp;" + "<span " + styles + ">" + status + "</span>");
        return;
    } else if (!(/^[1-9]\d*$/.test(overallSituationData.numberOfPatients))) {
        status = "请正确填写患者数量";
        $("#div").html("<div " + style + "></div>&nbsp;" + "<span " + styles + ">" + status + "</span>");
        return;
    } else if (overallSituationData.controlGroup == null || overallSituationData.controlGroup == "") {
        status = "请填写对照组";
        $("#div").html("<div " + style + "></div>&nbsp;" + "<span " + styles + ">" + status + "</span>");
        return;
    } else if (overallSituationData.state == "请选择") {
        status = "请选择状态";
        $("#div").html("<div " + style + "></div>&nbsp;" + "<span " + styles + ">" + status + "</span>");
        return;
    } else if (overallSituationData.background == null || overallSituationData.background == "") {
        status = "请填写背景";
        $("#div").html("<div " + style + "></div>&nbsp;" + "<span " + styles + ">" + status + "</span>");
        return;
    } else if (!(/^[\u4e00-\u9fa5]{0,}$/.test(overallSituationData.background))) {
        status = "请正确填写背景";
        $("#div").html("<div " + style + "></div>&nbsp;" + "<span " + styles + ">" + status + "</span>");
        return;
    } else {
        status = null;
        overallSituationData.deleteState = "1";
    }
    if (status == null) {
        $.ajax({
            url: "http://localhost:8989/hospital/FollowUpGroup/Update",
            type: "post",
            data: overallSituationData,
            dataType: "json",
            success: function (status) {
                if (status == true) {
                    $("#column").html("<tr><td style='color: #00cc03'>修改成功</td></tr>");
                    $("#myModal2").modal('hide');
                    //获取angular的controller 调用方法
                    var $scope = angular.element('#fenZuGuanLiView').scope();
                    //调用查询方法
                    $scope.TablesAndPagination();
                    setTimeout(function () {
                        $("#column").empty();
                    }, 5000)
                }
            }
        })
    }
}